package com.todo.event.activity.application.backend.controller;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.event.activity.application.core.eventflow.ActivityExecute;
import com.todo.event.activity.application.core.eventflow.DecisionExecute;
import com.todo.event.activity.application.entity.context.ActivityExecuteContext;
import com.todo.event.activity.application.entity.context.DecisionExecuteContext;
import com.todo.event.activity.common.enums.EventClassifyEnum;
import com.todo.event.activity.common.enums.EventTypeEnum;
import com.todo.event.activity.iface.backend.EventFlowTestIface;
import com.todo.event.activity.iface.backend.common.test.EventActivityTestReq;
import com.todo.event.activity.iface.backend.common.test.EventDecisionTestReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/1/14 11:17
 * @description
 */
@RestController
@RequestMapping("/bd/event/test")
@Tag(name = "事件活动后门测试入口", description = "事件活动后门测试入口")
public class EventFlowTestController implements EventFlowTestIface {

    private static final Logger log = LoggerFactory.getLogger(EventFlowTestController.class);
    @Resource
    private DecisionExecute decisionExecute;
    @Resource
    private ActivityExecute activityExecute;


    @Override
    @Operation(summary = "事件决策测试后门-线上不可用")
    @PostMapping("/eventDecisionTest")
    public BizResponse<Boolean> eventDecisionTest(@RequestBody EventDecisionTestReq req) {
        DecisionExecuteContext context = new DecisionExecuteContext();
        context.setEventClassify(EventClassifyEnum.getByType(req.getEventClassify()));
        context.setEventType(EventTypeEnum.getByType(req.getEventType()));
        context.setTopicName(req.getTopicName());
        context.setTag(req.getTag());
        context.setPayload(JSONObject.parseObject(req.getPayload()));
        Boolean execute = decisionExecute.execute(context);
        log.info("EventFlowTestController#eventDecisionTest 执行完成 入参：【{}】结果：【{}】", JSONObject.toJSONString(context), execute);
        return BizResponse.success(execute);
    }

    @Override
    @Operation(summary = "事件活动测试后门-线上不可用")
    @PostMapping("/eventActivityTest")
    public BizResponse<Boolean> eventActivityTest(@RequestBody EventActivityTestReq req) {
        ActivityExecuteContext context = new ActivityExecuteContext();
        context.setDecisionId(req.getDecisionId());
        context.setDecisionPayload(req.getDecisionPayload());
        context.setOperateTime(req.getOperateTime());
        context.setUserId(req.getUserId());
        context.setUk(req.getUk());
        Boolean execute = activityExecute.execute(context);
        log.info("EventFlowTestController#eventActivityTest 执行完成 入参：【{}】结果：【{}】", JSONObject.toJSONString(context), execute);
        return BizResponse.success(execute);
    }
}
